<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \DB;
use \Hash;
use App\Http\Requests;
use App\Http\Requests\UserAddPostRequest;
use App\Http\Requests\EditPassPostRequest;
use App\Http\Controllers\Controller;

use Germey\Geetest\CaptchaGeetest;
use Illuminate\Support\Facades\Config;

class LoginController extends Controller
{
    use CaptchaGeetest;
	//显示后台登录页面
	public function getLogin()
	{
		// 解析模板
		return view('admin.login');
	}

	// 后台登录验证
	public function postCheck(Request $request)
	{


		// 查询用户是否存在
		$user=DB::table('adminuser')->where('username',$request->input('loginuser'))->first();
    	// 验证用户名
    	if($user){
    		// 验证密码
    		if(Hash::check($request->input('loginpwd'),$user->userpassword)){
    			// 将id存入session中
    			session(['id'=>$user->id,'username'=>$user->username]);
    			//备注获取登录的时间 将时间插入数据库
    			$id=$user->id;

    			$timeing=date('Y-m-d H:i:s',time());
    			DB::table('adminuser')
		            ->where('id',$id )
		            ->update(['userlogining' => $timeing]);
		            
    			return redirect('/admin')->with('success');
    		}else{
    			// 密码错误
    			return back()->with('error','用户名或密码错误');
    		}
    	}else{
    		// 用户名错误
    		return back()->with('error','用户名输入错误');
    	}

		$result = $this->validate($request, [
		      'geetest_challenge' => 'required|geetest',
		    ], [
		      'geetest' => Config::get('geetest.server_fail_alert')
		    ]);
		    
    }
    // 前台登录验证
    public function postChecks(Request $request)
    {

       
        // 先根据获得的值判断是手机还是邮箱
        $a=$request->input('value');
        $resone=DB::table('homeuser')->where('homeuseremail',$a)->get();
        $restwo=DB::table('homeuser')->where('homeusertel',$a)->get();


        if ($resone==null && $restwo==null) {
           return back()->with('error','请输入正确的账号');
        }
        if ( $resone== null && !($restwo==null)  ){
          foreach ($restwo as $k => $v) {
              $idvalue=$v->id;
          }
        }
         if (  $restwo==null && !($resone==null)  ) {
           foreach ($resone as $k => $v) {
              $idvalue=$v->id;
          }
        }

        // 查询用户是否存在
        $user=DB::table('homeuser')->where('id',$idvalue)->first();
        // 验证用户名
        if($user){
            // 验证密码
            if(Hash::check($request->input('password'),$user->homeuserpassword)){
                // 将id存入session中
                session(['id'=>$user->id,'homeuseremail'=>$user->homeuseremail]);

                    
                return redirect('/')->with('success');
            }else{
                // 密码错误
                return back()->with('error','用户名或密码错误');
            }
        }else{
            // 用户名错误
            return back()->with('error','账号输入错误');
        }

        $result = $this->validate($request, [
              'geetest_challenge' => 'required|geetest',
            ], [
              'geetest' => Config::get('geetest.server_fail_alert')
            ]);
            
    }


	    // 后台退出登陆
    public function getLoginout(Request $request)
    {
        $sessiondata = $request->session()->all();
        $sessionid=$sessiondata['id'];
        $sessionres=DB::table('adminuser')->where('id',$sessionid)->get();   
            foreach ($sessionres as $k => $v) {
                $timea = $v->userlogining;
            }
            DB::table('adminuser')
                    ->where('id',$sessionid )
                    ->update(['userlastlogin' => $timea]);

      

        session()->pull('id','username');
    	return redirect('/admin/login/login');
    }

    // 前台退出登录
      public function getLoginsout(Request $request)
    {
       
        session()->flush();
        return redirect('/');
       
    }


}
